layui.use(['form','table','element','laytpl','layer','upload'], function() {
  var table = layui.table;
  var upload = layui.upload;
  table.render({
    elem: '#supplierTable2'
    ,url:'/ssm/order/queryListByCompany.do'
    ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
    ,page : true
    ,cols: [[
      {field:'orderNo', width:180, title: '订单编号'}
      ,{field:'recipients', title: '收货人'}
      ,{field:'tel', title: '电话'}
      ,{field:'address', title: '收货地址'}
      ,{field:'state', title: '订单状态',templet:function(d){
    	  var state = d.state==0?'订单已取消':(d.state==1?'待发货':(d.state==2?'待收货':'已收货'));
          return state;
      }}
      ,{field:'money', title: '总额'}
      ,{field:'ordertime', title: '创建时间'}
      ,{align:'center', toolbar: '#barTpl2', width: 120, title: '操作'}
    ]]
  });
  

  //工具条点击事件
  	layui.table.on('tool(table)', function(obj){
  		var data = obj.data;
  		var layEvent = obj.event;

  		if(layEvent === 'edit'){ //修改
  			showEditModel(data);
  		} else if(layEvent === 'send'){ //删除
  			doDelete(data);
  		} else if(layEvent == 'detail'){
  			showPermDialog(data);
  		}else if(layEvent == 'operateAuth'){
  			perAuthWin(data.id,data.role_name);
  		}
  	});


  	//添加按钮点击事件
  	$("#addBtn").click(function(){
  		showEditModel(null);
  		return false;
  	});

  

  	//显示表单弹窗
    function showEditModel(data){

    }
    
    function showPermDialog(data){
    	 layer.open({
             type: 1,
             title: data=="详情",
             area: '90%',
             offset: 'auto',
             content: $("#detailModel").html()
         });
         if(data!=null){
        	 table.render({
     		    elem: '#supplierTable3'
     		    ,url:'/ssm/order/queryOrderProduct.do'
     		    ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
     		    ,page : true
     		    ,cols: [[
     		      {field:'orderNo', width:180, title: '订单编号'}
     		      ,{field:'recipients', title: '收货人'}
     		      ,{field:'tel', title: '电话'}
     		      ,{field:'address', title: '收货地址'}
     		      ,{field:'state', title: '订单状态',templet:function(d){
     		    	  var state = d.state==0?'订单已取消':(d.state==1?'待发货':(d.state==2?'待收货':'已收货'));
     		          return state;
     		      }}
     		      ,{field:'money', title: '总额'}
     		      ,{field:'ordertime', title: '创建时间'}
     		    ]]
     		  });
         }
         $("#btnCancel").click(function(){
             layer.closeAll('page');
         });
    }

    	//表单提交事件
    layui.form.on('submit(btnSubmit)', function(data){
            var qUrl="/ssm/order/save.do";
            $.post(qUrl,{
                cid : $("#cid").val(),
                cname : $("#cname").val(),
            },function(res){
             if(res.code=="01"){
                 layer.closeAll();
                 layer.msg("保存成功");
                 //刷新父页面
                 layui.table.reload('supplierTable', {});
             }else{
                 layer.msg("保存失败");
             }
         });
            return false;
    });

    
	//删除
	function doDelete(data){
        layer.confirm('是否确认发货？',{icon:3, title:'提示信息'},function(index){
            $.post("/ssm/order/updateStatus.do",{
            		oid : data.oid,
            		state:2,
            },function(data){
                if(data.code=="01"){
                	layer.close(index);
                	layui.table.reload('supplierTable', {});
                    
                }else{
                	layer.close(index);
                    layer.alert(data.body);
                    
                }

            })
        });
	}

});